<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
    <el-form-item label="船员ID" v-show="false">
      <el-input v-model="dataForm.crewId" placeholder="船员ID" class="width400"></el-input>
    </el-form-item>
    <el-form-item label="证书名称" prop="certificateName">
      <el-input v-model="dataForm.certificateName" placeholder="证书名称" class="width400"></el-input>
    </el-form-item>
    <el-form-item label="证书号码" prop="certificateNumber">
      <el-input v-model="dataForm.certificateNumber" placeholder="证书号码" class="width400"></el-input>
    </el-form-item>
    <el-form-item label="签发时间" prop="certificateTime">
      <el-date-picker
        class="width400"
        v-model="dataForm.certificateTime"
        value-format="yyyy-MM-dd"
        type="date"
        placeholder="签发时间"
      ></el-date-picker>
    </el-form-item>
    <el-form-item label="到期时间" prop="expirationDate">
      <el-date-picker
        class="width400"
        v-model="dataForm.expirationDate"
        value-format="yyyy-MM-dd"
        type="date"
        placeholder="到期时间"
      ></el-date-picker>
    </el-form-item>
    <el-form-item label="签发地" prop="placeIssue">
      <el-input v-model="dataForm.placeIssue" placeholder="签发地" class="width400"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="certificateState">
      <!-- <el-input v-model="dataForm.certificateState" placeholder="状态" class="width400"></el-input> -->
      <el-select v-model="dataForm.certificateState" class="width400" placeholder="请选择">
        <el-option
          v-for="item in optionsCertificateState"
          :key="item.id"
          :label="item.value"
          :value="item.value"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="备注" prop="certificateRemarks">
      <el-input v-model="dataForm.certificateRemarks" placeholder="备注" type="textarea"  class="width400"></el-input>
    </el-form-item>
    <!-- <el-form-item label="创建人" prop="createUserId">
      <el-input v-model="dataForm.createUserId" placeholder="创建人" class="width400"></el-input>
    </el-form-item>
    <el-form-item label="所属公司" prop="affiliatedCompanyId">
      <el-input v-model="dataForm.affiliatedCompanyId" placeholder="所属公司" class="width400"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-date-picker
        class="width400"
        v-model="dataForm.createTime"
        value-format="yyyy-MM-dd"
        type="date"
        placeholder="创建时间"
      ></el-date-picker>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        optionsCertificateState: [
          {
            value: '有效',
            label: '有效'
          },{
            value: '过期',
            label: '过期'
          },{
            value: '作废',
            label: '作废'
          }
        ],
        dataForm: {
          certificateId: 0,
          crewId: '',
          certificateName: '',
          certificateNumber: '',
          certificateTime: '',
          expirationDate: '',
          placeIssue: '',
          certificateState: '',
          certificateRemarks: '',
          createUserId: '',
          affiliatedCompanyId: '',
          createTime: ''
        },
        dataRule: {
          // crewId: [
          //   { required: true, message: '船员ID不能为空', trigger: 'blur' }
          // ],
          certificateName: [
            { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
          ],
          certificateNumber: [
            { required: true, message: '证书号码不能为空', trigger: 'blur' },
            { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
          ],
          // certificateTime: [
          //   { required: true, message: '签发时间不能为空', trigger: 'blur' }
          // ],
          expirationDate: [
            { required: true, message: '到期时间不能为空', trigger: 'blur' }
          ],
          placeIssue: [
            { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
          ],
          certificateState: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          certificateRemarks: [
            { min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur' }
          ],
          // createUserId: [
          //   { required: true, message: '创建人不能为空', trigger: 'blur' }
          // ],
          // affiliatedCompanyId: [
          //   { required: true, message: '所属公司不能为空', trigger: 'blur' }
          // ],
          // createTime: [
          //   { required: true, message: '创建时间不能为空', trigger: 'blur' }
          // ]
        }
      }
    },
    methods: {
      init (id, add) {
      console.log('zhengenru',id, add)
        this.isadd = add
        this.dataForm.certificateId = id || 0
        this.visible = true
        this.$nextTick(() => {
          if (add === 0) {
            this.dataForm.crewId = id
            this.dataForm = {
            certificateId: 0,
            crewId: id,
            certificateName: '',
            certificateNumber: '',
            certificateTime: '',
            expirationDate: '',
            placeIssue: '',
            certificateState: '',
            certificateRemarks: '',
            createUserId: '',
            affiliatedCompanyId: '',
            createTime: ''
          }
          } else {
            this.$refs['dataForm'].resetFields()
            if (this.dataForm.certificateId) {
              this.$http({
                url: this.$http.adornUrl(`/generator/certificatemanagement/info/${this.dataForm.certificateId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm.crewId = data.certificateManagement.crewId
                  this.dataForm.certificateName = data.certificateManagement.certificateName
                  this.dataForm.certificateNumber = data.certificateManagement.certificateNumber
                  this.dataForm.certificateTime = data.certificateManagement.certificateTime
                  this.dataForm.expirationDate = data.certificateManagement.expirationDate
                  this.dataForm.placeIssue = data.certificateManagement.placeIssue
                  this.dataForm.certificateState = data.certificateManagement.certificateState
                  this.dataForm.certificateRemarks = data.certificateManagement.certificateRemarks
                  this.dataForm.createUserId = data.certificateManagement.createUserId
                  this.dataForm.affiliatedCompanyId = data.certificateManagement.affiliatedCompanyId
                  this.dataForm.createTime = data.certificateManagement.createTime
                }
              })
            }
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/certificatemanagement/${this.isadd === 0 ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'certificateId': this.dataForm.certificateId || undefined,
                'crewId': this.dataForm.crewId,
                'certificateName': this.dataForm.certificateName,
                'certificateNumber': this.dataForm.certificateNumber,
                'certificateTime': this.dataForm.certificateTime,
                'expirationDate': this.dataForm.expirationDate,
                'placeIssue': this.dataForm.placeIssue,
                'certificateState': this.dataForm.certificateState,
                'certificateRemarks': this.dataForm.certificateRemarks,
                'createUserId': this.dataForm.createUserId,
                'affiliatedCompanyId': this.dataForm.affiliatedCompanyId,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
